//
//  ITTrackManager.m
//  InTubePro
//
//  Created by seven on 2020/5/15.
//  Copyright © 2020 seven. All rights reserved.
//

#import "ITTrackManager.h"
#import "ITDeviceInfo.h"
#import <UIKit/UIKit.h>
//#import <GTSDK/GeTuiSdk.h>

@interface ITTrackManager(){
    CFAbsoluteTime _resignActiveTime;
    CFAbsoluteTime _appStartTime;
    CFAbsoluteTime _appBackgroundTime;
    CFAbsoluteTime _pageBackgroundTime;
    CFAbsoluteTime _pageBeginTime;
    
    
    CFAbsoluteTime _enterForegroundTIme;
    CFAbsoluteTime _thisBackgroundTime;
}
@end

@implementation ITTrackManager

+ (void)load{
    [[ITTrackManager shareManager] setupTrackTimes];
}

+ (instancetype)shareManager{
    static ITTrackManager *_manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _manager = ITTrackManager.new;
    });
    return _manager;
}

+ (void)trackMTAEventNo:(NSString *)eventNo{
    [[self shareManager] trackMTAEventNo:eventNo parameters:nil];
}

+ (void)trackMTAEventNo:(NSString *)eventNo parameters:(NSDictionary * IT_NULLABLE )parameters{
    [[self shareManager] trackMTAEventNo:eventNo parameters:parameters];
}

- (void)startApp{
    /*
    [FIRAnalytics setUserPropertyString:@"ios" forName:@"OS"];
    [FIRAnalytics setUserPropertyString:[ITDeviceInfo systemVersion] forName:@"OSVersion"];
    [FIRAnalytics setUserPropertyString:[ITDeviceInfo appVersion] forName:@"appVersion"];
    [FIRAnalytics setUserPropertyString:@"apple" forName:@"brand"];
    [FIRAnalytics setUserPropertyString:[ITDeviceInfo deviceModelName] forName:@"model"];
    [FIRAnalytics setUserPropertyString:[NSString stringWithUUID] forName:@"sessionId"];
    int memberDay = [NSDate dayCount] - [ITGeneralManager.defaultManager installationDay];
    [FIRAnalytics setUserPropertyString:@(memberDay).stringValue forName:@"memberDay"];
    
    [FIRAnalytics setUserPropertyString:ITDeviceInfo.country forName:@"country"];
    
    NSString * session_num_key = @"session_num_key";
    int session_num = [[NSUserDefaults getValueForKey:@"session_num_key"] intValue];
    session_num ++;
    [FIRAnalytics setUserPropertyString:@(session_num).stringValue forName:@"session_num"];
    [NSUserDefaults saveValue:@(session_num).stringValue forKey:session_num_key];
    
    //*/
    [self trackMTAEventNo:@"vq_00001" parameters:@{@"type": @(1)}];
    _enterForegroundTIme = CACurrentMediaTime();
}

- (void)updateNetworkStatus:(NSString *)status{
    //[FIRAnalytics setUserPropertyString:status forName:@"networkStatus"];
}

- (void)updateMemberId:(NSString *)memberId{
    //[FIRAnalytics setUserPropertyString:memberId forName:@"memberId"];
    //[GeTuiSdk bindAlias:memberId andSequenceNum:@"InVid"];
}

- (void)updateCampaign:(NSString *)campaign{
    if (campaign.length == 0) {
        return;
    }
    //[FIRAnalytics setUserPropertyString:campaign forName:@"campaign"];
}

- (void)updateGender:(NSString *)gender{
    //[FIRAnalytics setUserPropertyString:gender forName:@"gender"];
}

- (void)trackMTAEventNo:(NSString *)eventNo{
    [self trackMTAEventNo:eventNo parameters:nil];
}

- (void)trackMTAEventNo:(NSString *)eventNo parameters:(NSDictionary * IT_NULLABLE )parameters{
    GlobalDelay(0, ^{
        //NSDictionary *trackDic = [[ITUserManager shareManager] trackDictionaryWithEventNo:eventNo parameters:parameters];
        //[FIRAnalytics logEventWithName:eventNo parameters:parameters];
        /*
        NSMutableDictionary *umInfo = [NSMutableDictionary dictionaryWithDictionary:trackDic];
        [trackDic enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            if (![obj isKindOfClass:NSString.class]) {
                umInfo[key] = [NSString stringWithFormat:@"%@",obj];
            }
        }];
        //[MobClick event:eventNo attributes:umInfo];
         //*/
    });
}



- (void)enterBackground{
    int time =  CACurrentMediaTime()-_enterForegroundTIme-_thisBackgroundTime;
    [self trackMTAEventNo:@"vq_00002" parameters:@{@"duration": @(time)}];
}

- (void)enterForeground{
    _thisBackgroundTime = 0;
    _enterForegroundTIme = CACurrentMediaTime();
    [self trackMTAEventNo:@"vq_00001" parameters:@{@"type": @(2)}];
}

#pragma mark - app使用时长

//CFAbsoluteTime _resignActiveTime;
//CFAbsoluteTime _appStartTime;
//CFAbsoluteTime _appBackgroundTime;
//CFAbsoluteTime _pageBackgroundTime;_pageBeginTime
- (void)setupTrackTimes{
    
    _appStartTime = CACurrentMediaTime();
    
    [NSNotificationCenter addObserver:self selector:@selector(enterBackground) name:UIApplicationDidEnterBackgroundNotification];
    [NSNotificationCenter addObserver:self selector:@selector(enterForeground) name:UIApplicationWillEnterForegroundNotification];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resignActive) name:UIApplicationWillResignActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(becomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
    
}

- (long)appUsedTime{
    return (long)(CACurrentMediaTime() - _appStartTime - _appBackgroundTime);
}

- (void)resignActive{
    _resignActiveTime = CACurrentMediaTime();
}

- (void)becomeActive{
    if (_resignActiveTime <= 0) {
        return;
    }
    CFAbsoluteTime offset = CACurrentMediaTime() - _resignActiveTime;
    _appBackgroundTime += offset;
    _pageBackgroundTime += offset;
    _thisBackgroundTime += offset;
}

+ (void)setPageName:(NSString *)pageName{
    [ITTrackManager shareManager].pageName = pageName;
}

- (void)setPageName:(NSString *)pageName{
    _pageName = pageName;
    _pageBeginTime = CACurrentMediaTime();
    _pageBackgroundTime = 0;
}

- (long)pageShowedTime{
    return (long)(CACurrentMediaTime() - _pageBeginTime - _pageBackgroundTime);
}


@end
